<?php
require_once (APPLICATION_PATH . '/../library/phpthumblib/ThumbLib.inc.php');

class PropertyController extends GTL_Action {

    public function init() {
        parent::init();
        $this->lng_id = '1';
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('form', 'property', 'default');
    }

    public function listAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "prp_publish = '1'";
        $orderField = 'prp_id';
        $sort = 'asc';




        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchAllCountry("country_lang_id = '" . $this->lng_id . "'");

        $areaModel = new Model_Area();
        $areaArray = $areaModel->fetchAllArea("area_lang_id = '" . $this->lng_id . "'");

        $enoriaModel = new Model_Enoria();
        $enoriaArray = $enoriaModel->fetchAllEnoria("en_lang_id = '" . $this->lng_id . "'");

        $cityModel = new Model_City();
        $cityArray = $cityModel->fetchAllCity("city_lang_id = '" . $this->lng_id . "'");

        $propertyTypeModel = new Model_Propertytype();
        $propertyTypeArray = $propertyTypeModel->fetchAllPropertytype("prtyp_ln_id = '" . $this->lng_id . "'");


        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;

        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }


        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;


        $propertyModel = new Model_Property();
        //$propertyData = $propertyModel->fetchProperty($where);
        $propertyData = $propertyModel->fetchPropertyListing($where, $itemsPerPageReviews, $offset, $orderField, $sort);



        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($propertyModel->getPropertyCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'property', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->propertyData = $propertyData;


        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->countryArray = $countryArray;
        $this->view->propertyTypeArray = $propertyTypeArray;
        $this->view->areaArray = $areaArray;
        $this->view->enoriaArray = $enoriaArray;
        $this->view->cityArray = $cityArray;
    }

    public function listedAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "prp_publish = '1'";
        $orderField = 'prp_id';
        $sort = 'asc';




        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchAllCountry("country_lang_id = '" . $this->lng_id . "'");

        $areaModel = new Model_Area();
        $areaArray = $areaModel->fetchAllArea("area_lang_id = '" . $this->lng_id . "'");



        $enoriaModel = new Model_Enoria();
        $enoriaArray = $enoriaModel->fetchAllEnoria("en_lang_id = '" . $this->lng_id . "'");



        $cityModel = new Model_City();
        $cityArray = $cityModel->fetchAllCity("city_lang_id = '" . $this->lng_id . "'");

        $propertyTypeModel = new Model_Propertytype();
        $propertyTypeArray = $propertyTypeModel->fetchAllPropertytype("prtyp_ln_id = '" . $this->lng_id . "'");


        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;

        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }


        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;


        $propertyModel = new Model_Property();
        //$propertyData = $propertyModel->fetchProperty($where);
        $propertyData = $propertyModel->fetchPropertyListing($where, $itemsPerPageReviews, $offset, $orderField, $sort);



        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($propertyModel->getPropertyCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'property', 'action' => 'listed', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->propertyData = $propertyData;


        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->countryArray = $countryArray;
        $this->view->propertyTypeArray = $propertyTypeArray;
        $this->view->areaArray = $areaArray;
        $this->view->enoriaArray = $enoriaArray;
        $this->view->cityArray = $cityArray;
    }

    public function uploadAction() {

        $upload = new Zend_File_Transfer_Adapter_Http();

        foreach ($upload->getFileInfo() as $fields => $asFileInfo) {

            if ($asFileInfo['name'] != '') {
                switch ($fields) {
                    case "FYLLO_SXEDIO":
                        $folder = $this->config->UPLOAD_PATH . "property/fyllo_sxedio/";
                        break;
                    case "EXTRA_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/extraimages/";
                        break;
                    case "THREE_SIXTY_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/threesixtyimages/";
                        break;
                    case "FLOOR_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/floor/";
                        break;
                    case "BEDROOM_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/bedroom/";
                        break;
                    case "LIVING_ROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/living/";
                        break;
                    case "BATHROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/bathroom/";
                        break;
                    case "OUTDOOR":
                        $folder = $this->config->UPLOAD_PATH . "property/outdoor/";
                        break;
                    case "DOCUMENT":
                        $folder = $this->config->UPLOAD_PATH . "property/document/";
                        break;
                }

                if ($this->getRequest()->isPost()) {

                    $postedData = $this->_request->getPost();
                    if ($postedData['UNIQUEIDENTIFIER'] != '') {
                        $upload->setDestination($folder);
                        $originalFilename = pathinfo($asFileInfo['name']);
                        $file_name = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                        $upload->addFilter('Rename', $file_name, 'filename');
                        $upload->receive($fields);


                        if ($fields == 'DOCUMENT') {
                            $propertyDocumentModel = new Model_Propertydocument();
                            $propertyDocData = Array();
                            $propertyDocData['prp_id'] = '';
                            $propertyDocData['prpd_doc'] = $file_name;
                            $propertyDocData['prpd_type'] = $fields;
                            $propertyDocData['prpd_identifier'] = $postedData['UNIQUEIDENTIFIER'];
                            $propertyDocData['prpi_createddate'] = date('Y-m-d H:i:s');
                            $propertyDocumentModel->save($propertyDocData, 'prpi_id');
                        } else {

                            $propertyImageModel = new Model_Propertyimage();
                            $propertyImageData = Array();
                            $propertyImageData['prp_id'] = '';
                            $propertyImageData['prpi_img'] = $file_name;
                            $propertyImageData['prpi_type'] = $fields;
                            $propertyImageData['prpi_identifier'] = $postedData['UNIQUEIDENTIFIER'];
                            $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                            $propertyImageModel->save($propertyImageData, 'prpi_id');
                        }
                    }
                }
            }
        }


        exit;
    }

    public function formAction() {

       
	   //echo "<pre>"; 	   print_r($_POST); 	   exit;
	   
        $this->view->headTitle('LT Admin');
        $uniqueId = uniqid('unq_');

        $successmessage = "";
        $errorMessageArray = Array();
        $propertyKMLUpdateData = Array();
        $this->view->mode = "Add";
        $data = Array();
        $prpi_main_image = "";
        $prpi_fyllo_sxedio_image = "";
        $prp_hdn_subtype = "";
        $property_subtype = "";
        $zoneString = "";        
        $checked = "";
        
        
        
        $areaModel = new Model_Area();
        $enoriaModel = new Model_Enoria();
        $zoneModel = new Model_Zone();
        $cityModel = new Model_City();
        $vatModel = new Model_Vat();
        $markasModel = new Model_Markas();
        $markasdataModel = new Model_Markasdata();
        $energyclassModel = new Model_Energyclass();
        $shapeModel = new Model_Shape();
        $plotsizetypeModel = new Model_Plotsizetype();

        $propertyLogModel = new Model_Propertylog();

        $memberDetailsModel = new Model_Memberdetails();

        $markerArray = Array();

        $propertySubTypeModel = new Model_Propertysubtype();

        $kmlModel = new Model_Propertykml();
        $propertyImageModel = new Model_Propertyimage();
        $propertyDocumentModel = new Model_Propertydocument();
        $propertykmlcoordinatesModel = new Model_Propertykmlcoordinates();

        $prp_id = "";

        $propertyImages = $propertyImageModel->fetchPropertyImagesByPropertId();
        $propertyDocuments = $propertyDocumentModel->fetchPropertyDocumentsByPropertId();

        $languageModel = new Model_Language();
        $languagesArray = $languageModel->fetchlanguages();
        $languages = Array();
        foreach ($languagesArray as $key => $_lan) {
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']]['description'] = "";
        }

        $LATITUDE = $this->view->LATITUDE;
        $LONGITUDE = $this->view->LONGITUDE;

        $MARKERLATITUDE = '';
        $MARKERLONGITUDE = '';

        $memberModel = new Model_Member();
       
        $propertyForm = new Form_Property();

        $propertyModel = new Model_Property();
        
        $propertytagModel = new Model_Propertytag();

        $propertytagtypeModel = new Model_Propertytagtype();

        
        
        $allTagetypeData = $propertytagtypeModel->fetchtagwithsubcategory("prp_tgtype_lang_id = '" . $this->lng_id . "'" );
        $markasData = $markasModel->fetchAllMarkas("markas_lang_id = '" . $this->lng_id . "'" );         
                 
        /*$secondryData = $propertytagtypeModel->fetchPropertytagtype("prp_tgtype_lang_id = '" . $this->lng_id . "' and prp_tgtype_type = '1'");
        $indoorData = $propertytagtypeModel->fetchPropertytagtype("prp_tgtype_lang_id = '" . $this->lng_id . "' and prp_tgtype_type = '2'");
        $outdoorData = $propertytagtypeModel->fetchPropertytagtype("prp_tgtype_lang_id = '" . $this->lng_id . "' and prp_tgtype_type = '3'");
        $tenancyData = $propertytagtypeModel->fetchPropertytagtype("prp_tgtype_lang_id = '" . $this->lng_id . "' and prp_tgtype_type = '4'");

        $propertyForm->prp_secondry->addMultiOptions($secondryData);
        $propertyForm->prp_indoor->addMultiOptions($indoorData);
        $propertyForm->prp_outdoor->addMultiOptions($outdoorData);
        $propertyForm->prp_tenancy->addMultiOptions($tenancyData);
*/
            
        $propertyDescriptionModel = new Model_Propertydescription();


        //Load Member select box
        
        $memberArray = $memberModel->fetchMemberData();
        $propertyForm->member_id->addMultiOptions($memberArray);
        
        //Load property select box
        $propertytypeModel = new Model_Propertytype();
        $propertyTypeArray = $propertytypeModel->fetchPropertytype("prtyp_ln_id = '" . $this->lng_id . "'");
        $propertyForm->property_type->addMultiOptions($propertyTypeArray);


        //load vat in select box
        $propertyvatArray = $vatModel->fetchVat();
        $propertyForm->prp_vat_id->addMultiOptions($propertyvatArray);


        //load energy class in select box
        $propertyenergyclassArray = $energyclassModel->fetchEnergyclass();
        $propertyForm->prp_energy_cls_id->addMultiOptions($propertyenergyclassArray);

        //load title deed in select box
        $propertyForm->prp_title_deed->addMultiOptions($this->gettitledeed());

                
        //load Shape class in select box
        $propertyshapeArray = $shapeModel->fetchShape();
        $propertyForm->prp_shape_id->addMultiOptions($propertyshapeArray);

        //load plotsizetype in select box
        $propertyplotsizetypeArray = $plotsizetypeModel->fetchPlotsizetype();
        $propertyForm->prp_plot_size_type_id->addMultiOptions($propertyplotsizetypeArray);


        



        //Load country select box
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchCountry("country_lang_id = '" . $this->lng_id . "'");
//        $propertyForm->mem_country->addMultiOptions($countryArray);
        $propertyForm->prp_country->addMultiOptions($countryArray);
        $propertyForm->prp_enoria->addMultiOptions(array('' => '-- Select Enoria --'));
        //$propertyForm->prp_zone_id->addMultiOptions('');


        $propertyForm->prp_bedrooms->addMultiOptions($this->getbedrooms());
        $propertyForm->prp_bathrooms->addMultiOptions($this->getbathrooms());

        $propertyForm->prp_plotsize->addMultiOptions($this->getplotsize());

        $propertyForm->prp_floor->addMultiOptions($this->getfloor());



        //Check for the customer Parameter
        $propertyData['prp_id'] = $this->_request->getParam('prp_id');
        $prp_id = (!empty($propertyData['prp_id'])) ? $propertyData['prp_id'] : null;




        if (!empty($prp_id)) {

            //Set the Mode to Edit
            $this->view->mode = "Edit";

            //Fetch data to be loaded in form 
            $data = $propertyModel->fetchEntryById($prp_id);
           
           // $propertyForm->member_email->setAttrib('readonly', 'true');

           // $propertyForm->member_phone->setAttrib('readonly', 'true'); ##Phone Logic

            $prpi_main_image = $data['prp_image'];

          //  $cityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $data['mem_country'] . "'");
          //  $propertyForm->mem_city->addMultiOptions($cityArray);

            $propertySubTypeArray = $propertySubTypeModel->fetchPropertySubType("prstyp_ln_id = '" . $this->lng_id . "' and prtyp_id = '" . $data['property_type'] . "'");
            $propertyForm->property_subtype->addMultiOptions($propertySubTypeArray);


            $prpCityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $data['prp_country'] . "'");
            $propertyForm->prp_city->addMultiOptions($prpCityArray);

            $enoriaArray = $enoriaModel->fetchEnoria("en_lang_id = '" . $this->lng_id . "' and area_id = '" . $data['prp_city'] . "'");
            $propertyForm->prp_enoria->addMultiOptions($enoriaArray);

           // $zoneArray = $zoneModel->fetchZone("zone_lang_id = '" . $this->lng_id . "' and zone_area_id = '" . $data['prp_area'] . "'");
          //  $propertyForm->prp_zone_id->addMultiOptions($zoneArray);

            //fetch Zone data
                    $zoneArray = $zoneModel->fetchZone("zone_city_id = '" . $data['prp_city'] . "'");
                    $zoneString = '';
                    
                    if (count($zoneArray) > 0) {
					
					
					
						$zoneString = "
						<div class='row form-row'>
							<div class='col-md-10'>
								<label class='form-label'>Zone</label>
								<select name='prp_zone_id' id='prp_zone_id' class='form-control select2'>";
					
					/*
                        $zoneString = '<div class="panel-group" id="accordion"><div class="panel panel-default">
                                    <div class="panel-heading collapsed"> <h4 class="panel-title">
                                            <a class="collapsed item-class" data-toggle="collapse" data-parent="#accordion" href="#collapseone">
                                               Zone
                                            </a>
                                        </h4>
                                    </div> <div id="collapseone" class="panel-collapse collapse item-container">
                                                               <div class="panel-body"><div class="radio">';
                      */ 
					  
					  
					  //echo "<pre>"; print_r($zoneArray); exit;
					  
                        foreach ($zoneArray as $en_id => $_en_title) {
                           
							
                            if( $data['prp_zone_id'] == $_en_title['zone_id'] ){
                                $selected ="selected";
                            }else{
                                $selected ="";
                            }
							
							$zoneString .= "<option ".$selected." value='".$_en_title['zone_id']."'>". $_en_title['zone_zone'] ." " . $_en_title['zone_district'] . " " . $_en_title['zone_munic'] . " " . " " . $_en_title['zone_description']. " ". $_en_title['zone_max_max'] ." ".$_en_title['zone_max_cover'] ." ". $_en_title['zone_max_height']." ".$_en_title['zonr_max_floor']."</option>";
								
								/*
                            $zoneString .= '<div class="col-md-6 zone_cls">
                                          <div class="col-md-10 ">
                                          <input type="radio" '. $checked .'  value="'.$en_id.'" name="prp_zone_id" id="'.$_en_title['zone_zone'].'">
                                          <label for="'.$_en_title['zone_zone'].'">'.$_en_title['zone_zone'].'</label>
                                          </div>
                                            <div class="col-md-9">
                                            '.$_en_title['zone_district'].', '.$_en_title['zone_munic'].',
                                            '.$_en_title['zone_use'].', '.$_en_title['zone_description'].',
                                            '.$_en_title['zone_max_max'].', '.$_en_title['zone_max_cover'].',
                                            '.$_en_title['zone_max_height'].', '.$_en_title['zonr_max_floor'].'
                                            </div>
                                          
                                          </div>   
                                           ';
										   */
                        }

						$zoneString .= "</select>
							</div>
						</div>
					";
						
                        //$zoneString .= '</div></div></div></div></div>';
                        
                    }
                    
            $areaArray = $areaModel->fetchArea("area_lang_id = '" . $this->lng_id . "' and city_id = '" . $data['prp_city'] . "'");
            $propertyForm->prp_area->addMultiOptions($areaArray);

            $descriptionData = $propertyDescriptionModel->fetchDescriptionByPropertyId($prp_id);
                

            foreach ($descriptionData as $_desc) {
                if (array_key_exists($_desc['prp_lng_id'], $languages)) {
                    $languages[$_desc['prp_lng_id']]['description'] = $_desc['prd_description'];
                }
            }


            $tagdataArray = $propertytagModel->fetchTagByPropertyId($prp_id);
           
           
           /* $data['prp_secondry'] = $tagdataArray['SECONDRY'];
            $data['prp_indoor'] = $tagdataArray['INDOOR'];
            $data['prp_outdoor'] = $tagdataArray['OUTDOOR'];
            $data['prp_tenancy'] = $tagdataArray['TENANCY'];
            */
            
            $savedmarkasData =$markasdataModel->fetchMarkasByPropertyId("prpmark_ln_id = '" . $this->lng_id . "' and prpmark_prp_id = '".$prp_id."'" );
             
           
            $this->view->savedtag = $tagdataArray;
            $this->view->savedmarkasData = $savedmarkasData;
            
          
          
            
            $propertyImages = $propertyImageModel->fetchPropertyImagesByPropertId($prp_id);

            $propertyDocuments = $propertyDocumentModel->fetchPropertyDocumentsByPropertId($prp_id);

            $markerArray = $propertykmlcoordinatesModel->fetchPropertyKmlCoordinatesByPrpId($prp_id);


            $LATITUDE = $data['prp_lat'];
            $LONGITUDE = $data['prp_lon'];
            $prp_hdn_subtype = $data['property_subtype'];
            
            $rentprice = $data['prp_rentprice'];
            $saleprice = $data['prp_saleprice'];
          
            
        }

        $this->view->form = $propertyForm->populate($data);

        
        if ($this->getRequest()->isPost()) {

            //Check for Errors
            $postedData = $this->_request->getPost();

            
           
          if (array_key_exists('property_subtype', $postedData)) {
            $prp_hdn_subtype = $postedData['property_subtype'];
          }
          if (array_key_exists('prp_markas', $postedData)) {
            //$markasModel->fetchAllMarkas("markas_id = '" . $postedData['prp_markas'] . "'" );   
            $this->view->postmarkas = $postedData['prp_markas'];
            
          }
         
         // exit;


            if ($postedData['UNIQUEIDENTIFIER'] != '') {
                $uniqueId = $postedData['UNIQUEIDENTIFIER'];
            }

           /* if ($postedData['mem_country'] != '') {
                $cityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $postedData['mem_country'] . "'");
                $propertyForm->mem_city->addMultiOptions($cityArray);
            }*/

            if ($postedData['prp_country'] != '') {

                $prpCityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $postedData['prp_country'] . "'");
                $propertyForm->prp_city->clearMultiOptions();
                $propertyForm->prp_city->addMultiOptions($prpCityArray);
            }

            if ($postedData['property_type'] != '') {
                $propertySubTypeArray = $propertySubTypeModel->fetchPropertySubType("prstyp_ln_id = '" . $this->lng_id . "' and prtyp_id = '" . $postedData['property_type'] . "'");
                $propertyForm->property_subtype->clearMultiOptions();
                $propertyForm->property_subtype->addMultiOptions($propertySubTypeArray);
            }

            if ($postedData['prp_lat'] != '') {
                $LATITUDE = $postedData['prp_lat'];
            }

            if ($postedData['prp_lon'] != '') {
                $LONGITUDE = $postedData['prp_lon'];
            }

            if (array_key_exists('prp_city', $postedData)) {
                if ($postedData['prp_city'] != '') {

                    $areaArray = $areaModel->fetchArea("area_lang_id = '" . $this->lng_id . "' and city_id = '" . $postedData['prp_city'] . "'");
                    $propertyForm->prp_area->clearMultiOptions();
                    $propertyForm->prp_area->addMultiOptions($areaArray);
                    
                    
                     //fetch Zone data 11
                    $zoneArray = $zoneModel->fetchZone("  zone_city_id = '" . $postedData['prp_city'] . "'");
                   // $zoneString = '';
                    if (count($zoneArray) > 0) {
                      
                        $zoneString = '<div class="panel-group" id="accordion"><div class="panel panel-default">
                                    <div class="panel-heading collapsed"> <h4 class="panel-title">
                                            <a class="collapsed item-class" data-toggle="collapse" data-parent="#accordion" href="#collapseone">
                                               Zone
                                            </a>
                                        </h4>
                                    </div> <div id="collapseone" class="panel-collapse collapse item-container">
                                                               <div class="panel-body"><div class="radio">';
                        foreach ($zoneArray as $en_id => $_en_title) {
                           
                            if(array_key_exists('prp_zone_id', $postedData)){
                                if($postedData['prp_zone_id'] == $en_id){
                                    $checked ="checked";
                                }else{
                                    $checked ="";
                                }
                            }else{
                                $checked = "";
                            }
                            
                            $zoneString .= '<div class="col-md-6 zone_cls">
                                          <div class="col-md-10 ">
                                          <input type="radio"  '.$checked.' value="'.$en_id.'" name="prp_zone_id" id="'.$_en_title['zone_zone'].'">
                                          <label for="'.$_en_title['zone_zone'].'">'.$_en_title['zone_zone'].'</label>
                                          </div>
                                          
                                          <div class="col-md-9">
                                          '.$_en_title['zone_district'].', '.$_en_title['zone_munic'].',
                                          '.$_en_title['zone_use'].', '.$_en_title['zone_description'].',
                                          '.$_en_title['zone_max_max'].', '.$_en_title['zone_max_cover'].',
                                          '.$_en_title['zone_max_height'].', '.$_en_title['zonr_max_floor'].'
                                            </div>
                                          </div>   
                                           ';
                        }

                        $zoneString .= '</div></div></div></div>
                        </div>';
                    }

                }
                
               
            }
            
            if (array_key_exists('prp_area', $postedData)) {
                if ($postedData['prp_area'] != '') {

                    $enoriaArray = $enoriaModel->fetchEnoria("en_lang_id = '" . $this->lng_id . "' and area_id = '" . $postedData['prp_area'] . "'");
                    $propertyForm->prp_enoria->clearMultiOptions();
                    $propertyForm->prp_enoria->addMultiOptions($enoriaArray);
                    
                    
                   
                }
                
               
            }

            if (!$propertyForm->isValid($postedData)) {
                $errorMessage = $propertyForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {

                //KML Validation
                if (array_key_exists('kml', $_FILES)) {
                    if ($_FILES['kml']['name'] != '') {
                        $gtlObj = new GTL_Common();
                        $imageAllowedFormat = $gtlObj->allowedImageFormats();
                        $responseText = $gtlObj->image_validate(Array('application/vnd.google-earth.kml+xml'), 'kml');
                        if ($responseText == 'IMPROPER_FORMAT') {
                            $errorMessageArray[] = 'Please upload KML file.';
                        }
                    }
                }


                //Main Image Validation
                if (array_key_exists('prpi_main_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_main_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload main image with valid file format.';
                    }
                }


                //Fyllo Sxedio
                if (array_key_exists('prpi_fyllo_sxedio', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_fyllo_sxedio');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload Fyllo Sxedio image with valid file format.';
                    }
                }

                //Extra Image Validation
                if (array_key_exists('prpi_extra_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_extra_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload Extra images with valid file format.';
                    }
                }



                //360 Image Validation
                if (array_key_exists('prpi_360_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_360_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload 360 images with valid file format.';
                    }
                }

                //Floor Image Validation
                if (array_key_exists('prpi_floor_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_floor_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload floor images with valid file format.';
                    }
                }

                if (array_key_exists('prpi_bedroom_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_bedroom_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload bedroom images with valid file format.';
                    }
                }

                if (array_key_exists('prpi_living_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_living_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload living images with valid file format.';
                    }
                }

                if (array_key_exists('prpi_bathroom_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_bathroom_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload bathroom images with valid file format.';
                    }
                }

                if (array_key_exists('prpi_outdoor_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_outdoor_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload outdoor images with valid file format.';
                    }
                }

                //Document Upload Logic
                if (array_key_exists('prpi_document', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->extension_validate(Array('ppt', 'pptx', 'xls', 'xlsx', 'odp', 'doc', 'docx', 'pdf', 'ppsx'), 'prpi_document');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload document with valid file format.';
                    }
                }

                if (count($errorMessageArray) > 0) {
                    
                }/* else {
                   /* $member_username = $postedData['member_username'];
                    if ($member_username == '') {
                        $usernameArray = explode("@", $postedData['member_email']);
                        $member_username = $usernameArray['0'];
                    }*/

                    ##Phone Logic ##Phone Logic
                    //Check for the email exist while Adding custoemr
                   /* $memberExist = $memberModel->checkEmailExist($postedData['member_email']);
                    $phoneExist = $memberModel->checkPhoneExist($postedData['member_phone']);


                    if ($phoneExist) {
                        //If phone number exist
                        $emailExist = $memberModel->checkEmailExist($postedData['member_email']);

                        if ($phoneExist->member_email == $postedData['member_email']) {
                            //User exist and assign that user to it

                            $memberData = Array();
                            $memberData['member_username'] = $member_username;
                            $memberData['member_id'] = $phoneExist->member_id;
                            $memberData['member_phone'] = $postedData['member_phone'];
                            $memberModel->save($memberData, 'member_id');

                            $member_id = $phoneExist->member_id;
                            $memberDetailsArray['memd_id'] = $phoneExist->memd_id;
                            $memberDetailsArray['mem_name'] = $postedData['mem_name'];
                            $memberDetailsArray['mem_address'] = $postedData['mem_address'];
                            $memberDetailsArray['mem_surname'] = $postedData['mem_surname'];
                            $memberDetailsArray['mem_country'] = $postedData['mem_country'];
                            $memberDetailsArray['mem_city'] = $postedData['mem_city'];
                            $memberDetailsArray['mem_mobile'] = $postedData['mem_mobile'];
                            $memberDetailsArray['mem_landline'] = $postedData['mem_landline'];
                            $memberDetailsArray['mem_work'] = $postedData['mem_work'];
                            $memberDetailsArray['mem_representative_name'] = $postedData['mem_representative_name'];
                            $memberDetailsArray['mem_representative_telephone1'] = $postedData['mem_representative_telephone1'];
                            $memberDetailsArray['mem_representative_telephone2'] = $postedData['mem_representative_telephone2'];
                            $memberDetailsArray['mem_representative_telephone3'] = $postedData['mem_representative_telephone3'];
                            $memberDetailsArray['member_id'] = $member_id;
                            $memberDetailsModel->save($memberDetailsArray, 'memd_id');
                        } else {
                            //Show error message that this phone already exists and is linked with another email and cannot be linked to entered email
                            $errorMessageArray[] = 'Phone already exists and is linked with another email and cannot be linked to entered email';
                        }
                    } else {
                        //If phone number donot exist
                        $emailExist = $memberModel->checkEmailExist($postedData['member_email']);
                        if ($emailExist) {
                            //Show error message this email is linked with another phone number, so try another email
                            $errorMessageArray[] = 'Entered email is linked with another phone number, so try another email';
                        } else {
                            //Write down the code to insert and add new member

                            $memberData = Array();
                            $generatedPasswrod = GTL_Common::generatePassword();
                            $memberData['member_username'] = $member_username;
                            $memberData['member_email'] = $postedData['member_email'];
                            $memberData['member_password'] = md5($generatedPasswrod);
                            $memberData['member_admin'] = 0;
                            $memberData['member_phone'] = $postedData['member_phone'];
                            $memberData['member_forgot'] = "";
                            $memberData['oauth_provider'] = "0";
                            $memberData['oauth_access_token'] = "";
                            $memberData['oauth_user_key'] = "";
                            $memberData['member_admin_level'] = "1";

                            $member_id = $memberModel->save($memberData, 'member_id');

                            if ($member_id > 0) {

                                $memberDetailsArray = Array();
                                $memberDetailsArray['mem_title'] = "";
                                $memberDetailsArray['mem_name'] = $postedData['mem_name'];
                                $memberDetailsArray['mem_address'] = $postedData['mem_address'];
                                $memberDetailsArray['mem_surname'] = $postedData['mem_surname'];
                                $memberDetailsArray['mem_country'] = $postedData['mem_country'];
                                $memberDetailsArray['mem_city'] = $postedData['mem_city'];


                                $memberDetailsArray['mem_mobile'] = $postedData['mem_mobile'];
                                $memberDetailsArray['mem_landline'] = $postedData['mem_landline'];
                                $memberDetailsArray['mem_work'] = $postedData['mem_work'];
                                $memberDetailsArray['mem_representative_name'] = $postedData['mem_representative_name'];
                                $memberDetailsArray['mem_representative_telephone1'] = $postedData['mem_representative_telephone1'];
                                $memberDetailsArray['mem_representative_telephone2'] = $postedData['mem_representative_telephone2'];
                                $memberDetailsArray['mem_representative_telephone3'] = $postedData['mem_representative_telephone3'];

                                $memberDetailsArray['mem_bday'] = date('Y-m-d');
                                $memberDetailsArray['member_id'] = $member_id;
                                $memberDetailsModel->save($memberDetailsArray, 'memd_id');
                            }
                        }
                    }
                }*/

                if (count($errorMessageArray) > 0) {
                    
                } else {

                    //log for price of sale and rent
                    if($this->view->mode == "Edit"){
                        $pricelogModel = new Model_pricelog();
                        $priceData = Array();
                        $priceData['prp_id'] = $prp_id;
                        $priceData['member_id'] = $postedData['member_id'];
                        $priceData['price_createddate'] = date('Y-m-d H:i:s');
                        
                        if($rentprice != $postedData['prp_rentprice']){
                            $priceData['price_type'] = 'Rent'; 
                            $priceData['price_value'] = $postedData['prp_rentprice'];
                            $pricelogModel->save($priceData, 'price_id');
                        }
                        if($saleprice != $postedData['prp_saleprice']){
                            $priceData['price_type'] = 'Sale'; 
                            $priceData['price_value'] = $postedData['prp_saleprice'];
                            $pricelogModel->save($priceData, 'price_id');
                        }
                       
                    }
                    
                    
                    //echo $member_id;
                    $propertyData = Array();
                    $propertyData['prp_id'] = $prp_id;
                    $propertyData['member_id'] = $postedData['member_id'];
                    $propertyData['property_type'] = $postedData['property_type'];
                    $propertyData['property_subtype'] = $postedData['property_subtype'];
                    $propertyData['prp_displayaddress'] = $postedData['prp_displayaddress'];
                    $propertyData['prp_address'] = $postedData['prp_address'];
                    $propertyData['prp_lat'] = $postedData['prp_lat'];
                    $propertyData['prp_lon'] = $postedData['prp_lon'];
                    $propertyData['prp_country'] = $postedData['prp_country'];
                    $propertyData['prp_city'] = $postedData['prp_city'];
                    $propertyData['prp_area'] = $postedData['prp_area'];
                    if(array_key_exists('prp_enoria', $postedData)){
                        //echo $postedData['prp_enoria'];exit;
                        $propertyData['prp_enoria'] = $postedData['prp_enoria'];
                    }
                    $propertyData['prp_bedrooms'] = $postedData['prp_bedrooms'];
                    $propertyData['prp_bathrooms'] = $postedData['prp_bathrooms'];
                    $propertyData['prp_saleprice'] = $postedData['prp_saleprice'];
                    $propertyData['prp_rentprice'] = $postedData['prp_rentprice'];
                    $propertyData['prp_plotsize'] = $postedData['prp_plotsize'];
                    $propertyData['prp_plotarea'] = $postedData['prp_plotarea'];
                   // $propertyData['prp_built_year'] = $postedData['prp_built_year'];
                    
					
					
                    $propertyData['prp_floor'] = $postedData['prp_floor'];
                    $propertyData['prp_entrynumber'] = $postedData['prp_entrynumber'];
                    $propertyData['prp_plotnumber'] = $postedData['prp_plotnumber'];
                    //$propertyData['prp_zone'] = $postedData['prp_zone'];
                    $propertyData['prp_frontage'] = $postedData['prp_frontage'];
                    $propertyData['prp_videotext'] = $postedData['prp_videotext'];
                    $propertyData['prp_image'] = $prpi_main_image;

                    //Added new code - 03-07-2014
                    $propertyData['prp_vat_id'] = $postedData['prp_vat_id'];
                    
                    if(array_key_exists('prp_zone_id', $postedData)){
                        $propertyData['prp_zone_id'] = $postedData['prp_zone_id'];
                    }
                    
                    $propertyData['prp_plot_size_type_id'] = $postedData['prp_plot_size_type_id'];
                    $propertyData['prp_built_year'] = $postedData['prp_built_year'];
                    
					
					
                    $propertyData['prp_coveredarea'] = $postedData['prp_coveredarea'];
                    $propertyData['prp_coveredverenda'] = $postedData['prp_coveredverenda'];
                    $propertyData['prp_uncovered_veranda_size'] = $postedData['prp_uncovered_veranda_size'];
					
		    $propertyData['prp_total_area_1'] = $propertyData['prp_coveredarea'] + $propertyData['prp_coveredverenda'];
		    //$propertyData['prp_total_area_1'] = $postedData['prp_total_area_1'];
					
                    $propertyData['prp_internal_coveredarea'] = $postedData['prp_total_area_1'];
                    
                    $propertyData['prp_office_area'] = $postedData['prp_office_area'];
                    $propertyData['prp_shoparea'] = $postedData['prp_shoparea'];
                    $propertyData['prp_apartment_area'] = $postedData['prp_apartment_area'];
                    $propertyData['prp_total_area_2'] = $postedData['prp_office_area'] + $postedData['prp_shoparea'] + $postedData['prp_apartment_area'];
                    
                    //calculation for price per sqaure(on submit take the price (rent or sale )and divide is by total area)
                    if($postedData['prp_total_area_1'] != ''){
                        if ($postedData['prp_rentprice'] == 0 || $postedData['prp_rentprice'] == 0 || $postedData['prp_total_area_1'] == 0){
                            $price_per_sqaure  = 0;
                        }else if( $postedData['prp_rentprice'] != ''){
                            $price_per_sqaure  = $postedData['prp_rentprice'] / $postedData['prp_total_area_1'];
                        }else if( $postedData['prp_saleprice'] != ''){
                            $price_per_sqaure  = $postedData['prp_saleprice'] / $postedData['prp_total_area_1'];
                        } 
                    }else if($postedData['prp_total_area_2'] != ''){
                        if ($postedData['prp_rentprice'] == 0 || $postedData['prp_rentprice'] == 0 || $postedData['prp_total_area_2'] == 0){
                            $price_per_sqaure  = 0;
                        }else if( $postedData['prp_rentprice'] != ''){
                            $price_per_sqaure  = $postedData['prp_rentprice'] / $postedData['prp_total_area_2'];
                        }else if( $postedData['prp_saleprice'] != ''){
                            $price_per_sqaure  = $postedData['prp_saleprice'] / $postedData['prp_total_area_2'];
                        }
                    }else{
                        $price_per_sqaure  = 0;
                    }
                    
                    
                    $propertyData['prp_price_per_square'] = $price_per_sqaure;
                    
                    
                    
                    $propertyData['prp_floor_height'] = $postedData['prp_floor_height'];
                    $propertyData['prp_basement_level'] = $postedData['prp_basement_level'];
                    $propertyData['prb_basement'] = $postedData['prb_basement'];
                    $propertyData['prp_mezzanine'] = $postedData['prp_mezzanine'];
                    $propertyData['prp_attic'] = $postedData['prp_attic'];
                    $propertyData['prp_title_deed'] = $postedData['prp_title_deed'];


                    $propertyData['prp_building_floors'] = $postedData['prp_building_floors'];
                    $propertyData['prp_furnished'] = $postedData['prp_furnished'];
                    $propertyData['prp_pets_allowed'] = $postedData['prp_pets_allowed'];
                    $propertyData['prp_covered_parking_space'] = $postedData['prp_covered_parking_space'];
                    $propertyData['prp_uncovered_parking_space'] = $postedData['prp_uncovered_parking_space'];
                    $propertyData['prp_storage'] = $postedData['prp_storage'];

                    $propertyData['prp_private_yard'] = $postedData['prp_private_yard'];
                    $propertyData['prp_energy_cls_id'] = $postedData['prp_energy_cls_id'];
                    $propertyData['prp_water'] = $postedData['prp_water'];
                    $propertyData['prp_electricity'] = $postedData['prp_electricity'];
                    $propertyData['prp_road_access'] = $postedData['prp_road_access'];
                    $propertyData['prp_telephone_line'] = $postedData['prp_telephone_line'];
                    $propertyData['prp_shape_id'] = $postedData['prp_shape_id'];
                    $propertyData['prp_post_code'] = $postedData['prp_post_code'];
                    
                    $propertyData['prp_roi'] = $postedData['prp_roi'];
                    $propertyData['prp_guarandeed_for_x'] = $postedData['prp_guarandeed_for_x'];
                    $propertyData['prp_current_income'] = $postedData['prp_current_income'];
                    //$propertyData['prp_video'] = $postedData['prp_video'];
                    $propertyData['prp_publish'] = '1';
                    
                    $prp_id = $propertyModel->save($propertyData, 'prp_id');

                    if ($prp_id > 0) {
                        $upload = new Zend_File_Transfer_Adapter_Http();
                        foreach ($upload->getFileInfo() as $fields => $asFileInfo) {

                            if ($asFileInfo['name'] != '') {
                                //Main Image
                                $is_prpi_main_image_present = strpos($fields, "prpi_main_image");
                                if ($is_prpi_main_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/mainimage/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);

                                    $prpi_main_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_main_image, 'filename');
                                    $upload->receive($fields);

                                     $thumb2 = PhpThumbFactory::create($folder . $prpi_main_image);
                                     $thumb2->resize(50, 50);
                                     $thumb2->save($folder . '/thumb/' . 'thumb_' . $prpi_main_image, $originalFilename['extension']);

                            
                                    $postedData['prp_image'] = $prpi_main_image;

                                    $propertyImageUpdateData = Array();
                                    $propertyImageUpdateData['prp_id'] = $prp_id;
                                    $propertyImageUpdateData['prp_image'] = $prpi_main_image;
                                    $propertyModel->save($propertyImageUpdateData, 'prp_id');

                                    if ($this->view->mode == "Edit") {
                                        $main_imagepath = $folder . $data['prp_image'];
                                        if (file_exists($main_imagepath)) {
                                            unlink($main_imagepath);
                                        }
                                    }
                                }

                                //Fyllo Sxedio
                                $is_prpi_fyllo_sxedio_present = strpos($fields, "prpi_fyllo_sxedio");
                                if ($is_prpi_fyllo_sxedio_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/fyllo_sxedio/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_fyllo_sxedio = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_fyllo_sxedio, 'filename');
                                    $upload->receive($fields);

                                    $FYLLO_SXEDIOArray = $propertyImageModel->fetchPropertyImages("prp_id = '" . $prp_id . "' and prpi_type = 'FYLLO_SXEDIO'");
                                    $propertyImageModel->delete("prp_id = '" . $prp_id . "' and prpi_type = 'FYLLO_SXEDIO'");
                                    foreach ($FYLLO_SXEDIOArray as $_FYLLO_SXEDIO) {
                                        $FYLLO_SXEDIOPath = $folder . $_FYLLO_SXEDIO['prpi_img'];
                                        if (file_exists($FYLLO_SXEDIOPath)) {
                                            unlink($FYLLO_SXEDIOPath);
                                        }
                                    }

                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_fyllo_sxedio;
                                    $propertyImageData['prpi_type'] = 'FYLLO_SXEDIO';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //Extra Images Code
                                $is_prpi_extra_image_present = strpos($fields, "prpi_extra_image");
                                if ($is_prpi_extra_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/extraimages/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_extra_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_extra_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_extra_image;
                                    $propertyImageData['prpi_type'] = 'EXTRA_IMAGE';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    
                                   
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //360 Images Code
                                $is_360_extra_image_present = strpos($fields, "prpi_360_image");
                                if ($is_360_extra_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/threesixtyimages/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_360_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_360_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_360_image;
                                    $propertyImageData['prpi_type'] = 'THREE_SIXTY_IMAGE';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //FLOOR Images Code
                                $is_floor_image_present = strpos($fields, "prpi_floor_image");
                                if ($is_floor_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/floor/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_floor_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_floor_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_floor_image;
                                    $propertyImageData['prpi_type'] = 'FLOOR_IMAGE';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }


                                // BED ROOM IMAGES
                                $is_bedroom_image_present = strpos($fields, "prpi_bedroom_image");
                                if ($is_bedroom_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/bedroom/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_bedroom_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_bedroom_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_bedroom_image;
                                    $propertyImageData['prpi_type'] = 'BEDROOM_IMAGE';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //LIVING ROOM  
                                $is_living_image_present = strpos($fields, "prpi_living_image");
                                if ($is_living_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/living/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_living_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_living_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_living_image;
                                    $propertyImageData['prpi_type'] = 'LIVING_ROOM';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //BATHROOM    
                                $is_bathroom_image_present = strpos($fields, "prpi_bathroom_image");
                                if ($is_bathroom_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/bathroom/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_bathroom_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_bathroom_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_bathroom_image;
                                    $propertyImageData['prpi_type'] = 'BATHROOM';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                // Outdoor images 
                                $is_outdoor_image_present = strpos($fields, "prpi_outdoor_image");
                                if ($is_outdoor_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/outdoor/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_outdoor_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_outdoor_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_outdoor_image;
                                    $propertyImageData['prpi_type'] = 'OUTDOOR';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }



                                // Documents
                                $is_documents_present = strpos($fields, "prpi_document");
                                if ($is_documents_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/document/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_document = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_document, 'filename');
                                    $upload->receive($fields);
                                    $propertyDocData = Array();
                                    $propertyDocData['prp_id'] = $prp_id;
                                    $propertyDocData['prpd_doc'] = $prpi_document;
                                    $propertyDocData['prpd_type'] = 'DOCUMENT';
                                    $propertyDocData['prpd_identifier'] = '';
                                    $propertyDocData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyDocumentModel->save($propertyDocData, 'prpi_id');
                                }

                                // BED ROOM IMAGES
                                $is_kml_present = strpos($fields, "kml");
                                if ($is_kml_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/kml/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);

                                    $kml = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $kml, 'filename');
                                    $upload->receive($fields);

                                    $target_path = $folder . $kml;

                                    if (file_exists($target_path)) {


                                        if ($this->view->mode == "Edit") {
                                            //Delete Logic of KML file and KML Co-ordinates
                                            $kmlFiles = $kmlModel->fetchPropertyKml("prp_id = '" . $prp_id . "'");
                                            foreach ($kmlFiles as $_kml) {
                                                $folder = $this->config->UPLOAD_PATH . "property/kml/";
                                                $kmlTargetPath = $folder . $_kml['prpk_file'];
                                                if (file_exists($kmlTargetPath)) {
                                                    unlink($kmlTargetPath);
                                                }
                                            }
                                            $kmlModel->delete("prp_id = '" . $prp_id . "'");
                                            $propertykmlcoordinatesModel->delete("prp_id = '" . $prp_id . "'");
                                        }


                                        $kmlData = Array();
                                        $kmlData['prp_id'] = $prp_id;
                                        $kmlData['prpk_file'] = $kml;
                                        $kmlData['prpk_createddate'] = date('Y-m-d H:i:s');
                                        $prpk_id = $kmlModel->save($kmlData, 'prpk_id');
                                        $coordinatesArray = GTL_Common::parseKMl($target_path);

                                        if (count($coordinatesArray) > 0) {
                                            foreach ($coordinatesArray as $key => $_coordiante) {

                                                if ($key == 0) {
                                                    $propertyKMlUpdateData['prp_id'] = $prp_id;
                                                    $propertyKMlUpdateData['prp_lat'] = $_coordiante['lat'];
                                                    $propertyKMlUpdateData['prp_lon'] = $_coordiante['lon'];
                                                }

                                                $propertykmlcoordinatesArray = Array();
                                                $propertykmlcoordinatesArray['prpkc_id'] = NULL;
                                                $propertykmlcoordinatesArray['prpk_id'] = $prpk_id;
                                                $propertykmlcoordinatesArray['prp_id'] = $prp_id;
                                                $propertykmlcoordinatesArray['prpkc_lat'] = $_coordiante['lat'];
                                                $propertykmlcoordinatesArray['prpkc_lon'] = $_coordiante['lon'];
                                                $propertykmlcoordinatesArray['prpkc_createddate'] = date('Y-m-d H:i:s');
                                                $propertykmlcoordinatesModel->save($propertykmlcoordinatesArray, 'prpkc_id');
                                            }
                                        }
                                    }
                                }
                            }
                        }


                        $propertyDescriptionModel->delete("prp_id = '" . $prp_id . "'");

                        /*
                          echo "<pre>";
                          print_R($languages);
                          print_R($postedData['prd_description']);
                          exit;
                         */
                        $i = 0;
                        foreach ($languages as $key => $_lang) {
                            if ($postedData['prd_description'][$i] != '') {
                                $propertyDescriptionData = Array();
                                $propertyDescriptionData['prp_id'] = $prp_id;
                                $propertyDescriptionData['prd_description'] = $postedData['prd_description'][$i];
                                $propertyDescriptionData['prp_lng_id'] = $_lang['lang_id'];
                                $propertyDescriptionModel->save($propertyDescriptionData, 'prd_id');
                            }
                            $i++;
                        }

                        $propertytagModel->delete("prp_id = '" . $prp_id . "'");
                       
                        if (array_key_exists('prp_alltag', $postedData)) {
                           
                            foreach ($postedData['prp_alltag'] as $_val) {
                                $propertyTagArray['prp_id'] = $prp_id;
                                $propertyTagArray['prp_ln_id'] = $this->lng_id;
                                $propertyTagArray['prp_tag'] = $_val;
                                $propertyTagArray['prp_type'] = $postedData['tag_cat_id_'.$_val];
                                $propertytagModel->save($propertyTagArray, 'prpt_id');
                            }
                        }

                        
                        
                        $markasdataModel->delete("prpmark_prp_id = '" . $prp_id . "'");
                        if (array_key_exists('prp_markas', $postedData)) {
                            foreach ($postedData['prp_markas'] as $_val) {
                                $propertyMarkasArray['prpmark_prp_id'] = $prp_id;
                                $propertyMarkasArray['prpmark_ln_id'] = $this->lng_id;
                                $propertyMarkasArray['prpmark_member_id'] = $postedData['member_id'];
                                $propertyMarkasArray['prpmark_markas_id'] = $_val;
                                $markasdataModel->save($propertyMarkasArray, 'prpmark_id');
                            }
                        }
                        
                        
                        /*if (array_key_exists('prp_indoor', $postedData)) {
                            foreach ($postedData['prp_indoor'] as $_val) {
                                $propertyTagArray['prp_id'] = $prp_id;
                                $propertyTagArray['prp_ln_id'] = $this->lng_id;
                                $propertyTagArray['prp_tag'] = $_val;
                                $propertyTagArray['prp_type'] = '2';
                                $propertytagModel->save($propertyTagArray, 'prpt_id');
                            }
                        }

                        if (array_key_exists('prp_outdoor', $postedData)) {
                            foreach ($postedData['prp_outdoor'] as $_val) {
                                $propertyTagArray['prp_id'] = $prp_id;
                                $propertyTagArray['prp_ln_id'] = $this->lng_id;
                                $propertyTagArray['prp_tag'] = $_val;
                                $propertyTagArray['prp_type'] = '3';
                                $propertytagModel->save($propertyTagArray, 'prpt_id');
                            }
                        }

                        if (array_key_exists('prp_tenancy', $postedData)) {
                            foreach ($postedData['prp_tenancy'] as $_val) {
                                $propertyTagArray['prp_id'] = $prp_id;
                                $propertyTagArray['prp_ln_id'] = $this->lng_id;
                                $propertyTagArray['prp_tag'] = $_val;
                                $propertyTagArray['prp_type'] = '4';
                                $propertytagModel->save($propertyTagArray, 'prpt_id');
                            }
                        }*/
                    }


                    if (count($propertyKMlUpdateData) > 0) {
                        $propertyModel->save($propertyKMlUpdateData, 'prp_id');
                    }

                    //Property Log Entry
                    $propertyLogArray = Array();
                    $propertyLogArray['prplg_id'] = NULL;
                    $propertyLogArray['prp_id'] = $prp_id;
                    $propertyLogArray['member_id'] = $postedData['member_id'];//$this->view->user->member_id;
                    
                    $propertyLogArray['prp_date'] = date('Y-m-d H:i:s');
                    
                    $propertyLogModel->save($propertyLogArray, "prplg_id");
                    //echo 'in';exit;
                    

                    $propertyImageModel->update(Array('prp_id' => $prp_id), "prpi_identifier = '" . $uniqueId . "'");
                    $propertyDocumentModel->update(Array('prp_id' => $prp_id), "prpd_identifier = '" . $uniqueId . "'");

                    if ($this->view->mode == "Add") {
                        $successmessage = "Property added successfully";
                    } else {
                        $successmessage = "Property updated successfully";
                    }

                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'property', '');
                }
            }
        }



        $this->view->prp_hdn_subtype = $prp_hdn_subtype;
        $this->view->prp_id = $prp_id;
        $this->view->propertyImages = $propertyImages;
        $this->view->propertyDocuments = $propertyDocuments;

        $this->view->prpi_main_image = $prpi_main_image;
        $this->view->languages = $languages;
        $this->view->MARKERLONGITUDE = $MARKERLONGITUDE;
        $this->view->MARKERLATITUDE = $MARKERLATITUDE;


        if (count($markerArray) > 0) {
            $this->view->iskmlpresent = "YES";
        } else {
            $this->view->iskmlpresent = "NO";
        }


        
         /* $this->view->secondryData = $secondryData;
          $this->view->indoorData = $indoorData;
          $this->view->outdoorData = $outdoorData;
          $this->view->tenancyData = $tenancyData;
          */
       
        
        $this->view->allTagetypeData = $allTagetypeData;
        $this->view->zoneString = $zoneString; 
        $this->view->markasData = $markasData;         
                 
        $this->view->UNIQUEIDENTIFIER = $uniqueId;
        $this->view->markerArray = json_encode($markerArray);
        $this->view->LATITUDE = $LATITUDE;
        $this->view->LONGITUDE = $LONGITUDE;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $propertyForm;
    }

    public function gettitledeed() {
        return array(
            '' => '-- Select Title Deed --',
            'yes' => 'Yes',
            'no' => 'No'
        );
    }

    public function getbedrooms() {
        $bedroomArray[] = "-- Select bedrooms --";
        foreach (range(1, 8) as $number) {
            if($number == 8){
                 $bedroomArray['8+'] = '8+';
            }else{
                $bedroomArray[$number] = $number;
            }
        }
        return $bedroomArray;
    }

    public function getbathrooms() {
        $bathroomArray[] = "-- Select bathrooms --";
        foreach (range(1, 5) as $number) {
             if($number == 5){
                 $bathroomArray['5+'] = '5+';
            }else{
                $bathroomArray[$number] = $number;
            }
        }
        return $bathroomArray;
    }

    public function getplotsize() {
        $plotArray[] = "-- Select plot size --";
        foreach (range(1, 10) as $number) {
            $plotArray[$number] = $number;
        }
        return $plotArray;
    }

    public function getfloor() {
        $floorArray[] = "-- Select floor --";
        foreach (range(1, 50) as $number) {
            $floorArray[$number] = $number;
        }
        return $floorArray;
    }

    public function getcitylistAction() {
        $cityString = "<option>-- Select City--</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['country_id'] != '') {

                $cityModel = new Model_City();
                $cityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $postedData['country_id'] . "'");

                if (count($cityArray) > 0) {
                    $cityString = "";
                    foreach ($cityArray as $city_id => $_city_title) {
                        $cityString .= "<option value='" . $city_id . "'>" . $_city_title . "</option>";
                    }
                }
            }
        }
        echo $cityString;
        exit;
    }

    public function getpropertysublistAction() {
        $propertySubString = "<option>-- Select Property subtype --</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['property_type'] != '') {
                $propertySubTypeModel = new Model_Propertysubtype();
                $propertySubTypeArray = $propertySubTypeModel->fetchPropertySubType("prstyp_ln_id = '" . $this->lng_id . "' and prtyp_id = '" . $postedData['property_type'] . "'");
                if (count($propertySubTypeArray) > 0) {
                    $propertySubString = "";
                    foreach ($propertySubTypeArray as $_id => $_title) {
                        $propertySubString .= "<option value='" . $_id . "'>" . $_title . "</option>";
                    }
                }
            }
        }
        echo $propertySubString;
        exit;
    }

    public function getarealistAction() {
       // $areaString = "<option>-- Select Area--</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['city_id'] != '') {
                $areaModel = new Model_Area();
                $areaArray = $areaModel->fetchArea("area_lang_id = '" . $this->lng_id . "' and city_id = '" . $postedData['city_id'] . "'");
                if (count($areaArray) > 0) {
                    $areaString = "";
                    foreach ($areaArray as $area_id => $_area_title) {
                        $areaString .= "<option value='" . $area_id . "'>" . $_area_title . "</option>";
                    }
                }
            }
        }
        echo $areaString;
        exit;
    }

    public function getenorialistAction() {
        $enoriaString = "<option value=''>-- Select Enoria --</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['area_id'] != '') {
                $enoriaModel = new Model_Enoria();
                $enoriaArray = $enoriaModel->fetchEnoria("en_lang_id = '" . $this->lng_id . "' and area_id = '" . $postedData['area_id'] . "'");
                if (count($enoriaArray) > 0) {
                    $enoriaString = "";
                    foreach ($enoriaArray as $en_id => $_en_title) {
                        $enoriaString .= "<option value='" . $en_id . "'>" . $_en_title . "</option>";
                    }
                }
            }
        }
        echo $enoriaString;
        exit;
    }
    
    
     public function zonelistAction() {
		
		$responseString = "";
		if ($this->getRequest()->isPost()) {
			$postedData = $this->_request->getPost();
			if ($postedData['city_id'] != '') {
				$zoneModel = new Model_Zone();
				$zoneArray = $zoneModel->fetchZone("  zone_city_id = '" . $postedData['city_id'] . "'");
				if (count($zoneArray) > 0) {
					$responseString = "
						<div class='row form-row'>
							<div class='col-md-10'>
								<label class='form-label'>Zone</label>
								<select name='prp_zone_id' id='prp_zone_id' class='form-control select2'>";
									foreach ($zoneArray as $en_id => $_en_title) {
										$responseString .= "<option value='".$_en_title['zone_id']."'>". $_en_title['zone_zone'] ." " . $_en_title['zone_district'] . " " . $_en_title['zone_munic'] . " " . " " . $_en_title['zone_description']. " ". $_en_title['zone_max_max'] ." ".$_en_title['zone_max_cover'] ." ". $_en_title['zone_max_height']." ".$_en_title['zonr_max_floor']."</option>";
									}
								$responseString .= "</select>
							</div>
						</div>
						<script>
							$('#prp_zone_id').select2();
						</script>
					";
				}
			}
		}
		
		
		
		echo $responseString;
		exit;
		
        $zoneString = '<div class="panel-group" id="accordion">';
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['city_id'] != '') {
                $zoneModel = new Model_Zone();
                $zoneArray = $zoneModel->fetchZone(" zone_city_id = '" . $postedData['city_id'] . "'");
                if (count($zoneArray) > 0) {
                    $zoneString = ' <div class="panel panel-default">
                                    <div class="panel-heading collapsed"> <h4 class="panel-title">
                                            <a class="collapsed item-class" data-toggle="collapse" data-parent="#accordion" href="#collapseone">
                                               Zone
                                            </a>
                                        </h4>
                                    </div> <div id="collapseone" class="panel-collapse collapse item-container">
                                                               <div class="panel-body"><div class="radio">';
                    
                    
                    foreach ($zoneArray as $en_id => $_en_title) {
                        $zoneString .= ' <div class="col-md-6 zone_cls">
                                          <div class="col-md-10 ">
                                          <input type="radio"  value="'.$en_id.'" name="prp_zone_id" id="'.$_en_title['zone_zone'].'">
                                          <label for="'.$_en_title['zone_zone'].'">'.$_en_title['zone_zone'].'</label>
                                          </div>
                                          
                                          <div class="col-md-9">
                                          '.$_en_title['zone_district'].', '.$_en_title['zone_munic'].',
                                          '.$_en_title['zone_use'].', '.$_en_title['zone_description'].',
                                          '.$_en_title['zone_max_max'].', '.$_en_title['zone_max_cover'].',
                                          '.$_en_title['zone_max_height'].', '.$_en_title['zonr_max_floor'].'
                                            </div>
                                          </div> 
                                       ';
                    }
                    
                    $zoneString .= '</div></div></div></div>
                        </div>';
                }
            }
        }
        echo $zoneString;
        exit;
    }
    
    
    
    

    public function archiveAction() {

        $propertyModel = new Model_Property();


        if ($this->_getParam('prp_id') && $this->_getParam('prp_id') != "") {
            $prp_id = $this->_getParam('prp_id');
            $propertyData = Array();
            $propertyData['prp_id'] = $prp_id;
            $propertyData['prp_publish'] = '0';

            $propertyModel->save($propertyData, 'prp_id');

            $successmessage = "Property archived successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('list', 'property', '');
            exit;
        }
    }

    public function deleteAction() {


        if ($this->_getParam('prp_id') && $this->_getParam('prp_id') != "") {
            $prp_id = $this->_getParam('prp_id');

            $propertyModel = new Model_Property();
            $kmlModel = new Model_Propertykml();
            $propertykmlcoordinatesModel = new Model_Propertykmlcoordinates();
            $propertyDescriptionModel = new Model_Propertydescription();
            $propertytagModel = new Model_Propertytag();
            $propertyImageModel = new Model_Propertyimage();
            $propertyDocumentModel = new Model_Propertydocument();

            $propertyDescriptionModel->delete("prp_id = '" . $prp_id . "'");

            $propertytagModel->delete("prp_id = '" . $prp_id . "'");



            $propertyImages = $propertyImageModel->fetchPropertyImages("prp_id = '" . $prp_id . "'");
            foreach ($propertyImages as $_images) {


                switch ($_images['prpi_type']) {
                    case "FYLLO_SXEDIO":
                        $folder = $this->config->UPLOAD_PATH . "property/fyllo_sxedio/";
                        break;
                    case "EXTRA_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/extraimages/";
                        break;
                    case "THREE_SIXTY_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/threesixtyimages/";
                        break;
                    case "FLOOR_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/floor/";
                        break;
                    case "BEDROOM_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/bedroom/";
                        break;
                    case "LIVING_ROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/living/";
                        break;
                    case "BATHROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/bathroom/";
                        break;
                    case "OUTDOOR":
                        $folder = $this->config->UPLOAD_PATH . "property/outdoor/";
                        break;
                    case "DOCUMENT":
                        $folder = $this->config->UPLOAD_PATH . "property/document/";
                        break;
                }
                $imagePath = $folder . $_images['prpi_img'];
                if (file_exists($imagePath)) {
                    unlink($imagePath);
                }
            }
            $propertyImageModel->delete("prp_id = '" . $prp_id . "'");

            $propertyDocuments = $propertyDocumentModel->fetchPropertyDocuments("prp_id = '" . $prp_id . "'");
            foreach ($propertyDocuments as $_doc) {
                $folder = $this->config->UPLOAD_PATH . "property/document/";
                $docPath = $folder . $_doc['prpd_doc'];
                if (file_exists($docPath)) {
                    unlink($docPath);
                }
            }
            $propertyDocumentModel->delete("prp_id = '" . $prp_id . "'");

            $propertyKmls = $kmlModel->fetchPropertyKml("prp_id = '" . $prp_id . "'");
            foreach ($propertyKmls as $_kml) {
                $folder = $this->config->UPLOAD_PATH . "property/kml/";
                $kmlPath = $folder . $_kml['prpk_file'];
                if (file_exists($kmlPath)) {
                    unlink($kmlPath);
                }
            }
            $kmlModel->delete("prp_id = '" . $prp_id . "'");
            $propertykmlcoordinatesModel->delete("prp_id = '" . $prp_id . "'");

            $data = $propertyModel->fetchEntryById($prp_id);
            $folder = $this->config->UPLOAD_PATH . "property/mainimage/";
            $main_imagepath = $folder . $data['prp_image'];
            if (file_exists($main_imagepath)) {
                unlink($main_imagepath);
            }
            $propertyModel->delete("prp_id = '" . $prp_id . "'");

            $successmessage = "Property deleted successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('list', 'property', '');
            exit;
        }
    }

    public function deleteimageAction() {

        $propertyModel = new Model_Property();
        $propertyImageModel = new Model_Propertyimage();

        $type = "";
        if ($this->_getParam('type') && $this->_getParam('type') != "") {
            $type = $this->_getParam('type');
        }

        if ($type == '') {
            $this->_helper->redirector('list', 'property', '');
            exit;
        }

        if ($this->_getParam('id') && $this->_getParam('id') != "") {
            $id = $this->_getParam('id');
        }

        if ($type == 'MAIN_IMAGE') {
            if ($this->_getParam('id') && $this->_getParam('id') != "") {
                $propertyImageUpdateData = Array();
                $propertyImageUpdateData['prp_id'] = $id;
                $propertyImageUpdateData['prp_image'] = "";
                $propertyData = $propertyModel->fetchEntryById($id);
                $prp_image = $propertyData['prp_image'];
                $folder = $this->config->UPLOAD_PATH . "property/mainimage/";
                $targetPath = $folder . $prp_image;
                if (file_exists($targetPath)) {
                    unlink($targetPath);
                }
                $propertyModel->save($propertyImageUpdateData, 'prp_id');
                $successmessage = "Property image deleted successfully";
                $this->addSuccessMessage($successmessage);
                $this->_helper->redirector('list', 'property', '');
                exit;
            }
        } else if ($type == 'DOCUMENT') {
            if ($this->_getParam('id') && $this->_getParam('id') != "") {
                $id = $this->_getParam('id');

                $propertyDocumentModel = new Model_Propertydocument();

                $documents = $propertyDocumentModel->fetchEntryById($id);
                $propertyDocumentModel->delete("prpd_id = '" . $id . "'");
                $folder = $this->config->UPLOAD_PATH . "property/document/";
                $targetPath = $folder . $documents['prpd_doc'];
                if (file_exists($targetPath)) {
                    unlink($targetPath);
                }
                $successmessage = "Property document deleted successfully";
                $this->_helper->redirector('list', 'property', '');
                exit;
            }
        } else {
            if ($this->_getParam('id') && $this->_getParam('id') != "") {
                $id = $this->_getParam('id');
                $propertyImage = $propertyImageModel->fetchEntryById($id);
                $type = $propertyImage['prpi_type'];
                switch ($type) {
                    case "FYLLO_SXEDIO":
                        $folder = $this->config->UPLOAD_PATH . "property/fyllo_sxedio/";
                        break;
                    case "EXTRA_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/extraimages/";
                        break;
                    case "THREE_SIXTY_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/threesixtyimages/";
                        break;
                    case "FLOOR_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/floor/";
                        break;
                    case "BEDROOM_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/bedroom/";
                        break;
                    case "LIVING_ROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/living/";
                        break;
                    case "BATHROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/bathroom/";
                        break;
                    case "OUTDOOR":
                        $folder = $this->config->UPLOAD_PATH . "property/outdoor/";
                        break;
                    case "DOCUMENT":
                        $folder = $this->config->UPLOAD_PATH . "property/document/";
                        break;
                }
                $prp_image = $propertyImage['prpi_img'];
                $targetPath = $folder . $prp_image;
                if (file_exists($targetPath)) {
                    unlink($targetPath);
                }
                $propertyImageModel->delete("prpi_id = '" . $id . "'");
                $successmessage = "Property image deleted successfully";

                $this->addSuccessMessage($successmessage);
                $this->_helper->redirector('list', 'property', '');
                exit;
            }
        }
    }

}

?>